﻿using MediatR;
using Microsoft.EntityFrameworkCore;
using Tb.App.API.Applications.Command.DeepProcessing;
using Tb.App.Domain.Rabbit.DeepProcessing;
using Tb.App.ErrorCode;
using Tb.App.Infrastructure.Interfaces;

namespace Tb.App.API.Applications.CommandHandler.DeepProcessing
{
    /// <summary>
    /// 深加工信息查询-处理
    /// </summary>
    public class GetDeeprocessingModelCommandHandler : IRequestHandler<GetDeeprocessingModelCommand, ApiResult<DeepProcessingModel>>
    {
        /// <summary>
        /// 深加工信息仓储
        /// </summary>
        private readonly IBaseRepository<DeepProcessingModel> deeprocessingModelRep;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="deeprocessingModelRep">深加工信息仓储</param>
        public GetDeeprocessingModelCommandHandler(IBaseRepository<DeepProcessingModel> deeprocessingModelRep)
        {
            this.deeprocessingModelRep = deeprocessingModelRep;
        }
        public async Task<ApiResult<DeepProcessingModel>> Handle(GetDeeprocessingModelCommand request, CancellationToken cancellationToken)
        {
            ApiResult<DeepProcessingModel> result=new ApiResult<DeepProcessingModel>();

            var entity = await deeprocessingModelRep.GetAll().Where(x => !x.IsDel && x.Batch == request.Batch).OrderByDescending(x => x.Id).FirstOrDefaultAsync();
            if(entity==null)
            {
                result.Code = ApiEnum.查询失败;
                result.Message = "未找到对应的深加工信息";
                result.Data = null;
            }
            else
            {
                result.Code = ApiEnum.查询成功;
                result.Message = "查询成功";
                result.Data = entity;
            }
            return result;
        }
    }
}
